<?php
/**
 * Created by PhpStorm.
 * User: wuz
 * Date: 2016/8/17
 * Time: 16:38
 */
session_start(); //一定要先注册会话！！！
header("content-type:text/html;charset=utf-8");
include "./Mysql/MySqlClass.php";
$mySQL = new Ycf\Lession\MySqlClass\MySqlClass('jinling_company', 'root', '123456');
if (@$_GET['action'] == "login") {
    if (empty($_POST['userName']) || empty($_POST['password'])) {
        header("Location: login.php");
    } else {
        $selectUserName = array('adminName' => $_POST['userName'],
            'adminPassword'                     => $_POST['password']);
        $userName = $mySQL->select("admin", $selectUserName); //通过用户名和密码查询数据库
        if (count($userName) != 0) { //返回的数组不为空则进入系统
            $_SESSION['userName'] = $_POST['userName'];
            $_SESSION['password'] = $_POST['password'];
            header("Location: index.php");
        } else {
            echo "密码错误";
        }
    }
    //var_dump($userName);

}
if (@$_GET['action'] == "logout") {
    if (isset($_SESSION['userName']) or isset($_SESSION['password'])) {
        $_SESSION[] = array();
        unset($_SESSION['userName']);
        unset($_SESSION['password']);
        if (isset($_COOKIE['userName'])) {
            setcookie('username', '', time() - 3600);
        }
        if (isset($_COOKIE['password'])) {
            setcookie('password', '', time() - 3600);
        }
        session_destroy(); //销毁会话
    }
    header("Location: login.php");
}
